<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="片区" prop="vo.companyBudgetArea">
        <el-select
          v-model="queryParams.vo.companyBudgetArea"
          placeholder="请选择片区"
          clearable
          size="small"
        >
          <el-option :key="item.deptId" v-for="item in pq" :label="item.shortName" :value="item.deptId" />
        </el-select>
      </el-form-item>

      <el-form-item label="公司" prop="vo.deptBudgetShortname">
        <el-input
          v-model="queryParams.vo.deptBudgetShortname"
          placeholder="请输入公司"
          clearable
          size="small"
        />
      </el-form-item>

      <!-- <el-form-item label="预算指标" prop="zb">
        <el-select
          v-model="queryParams.zb"
          placeholder="请选择预算指标"
          clearable
          size="small"
        >
          <el-option label="" value="" />
        </el-select>
      </el-form-item> -->

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!--option-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:budget:remove']"
          >删除</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:budget:export']"
        >导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
              type="info"
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['system:user:import']"
        >导入</el-button>
      </el-col>

    </el-row>

    <!--列表-->
    <el-table
      v-loading="loading"
      :data="budgetList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="片区" align="center" prop="deptBudgetArea" />

      <el-table-column
        label="公司简称"
        align="center"
        prop="deptBudgetShortname"
      />

      <el-table-column
        label="公司总经理"
        align="center"
        prop="deptBudgetManager"
      />

      <el-table-column
        label="服务部"
        align="center"
        prop="deptBudgetDept"
      />

      <el-table-column
        label="服务部经理"
        align="center"
        prop="deptBudgetDeptManager"
      />



      <el-table-column
        label="预算指标"
        align="center"
        prop="deptBudgetType"
      />

      <el-table-column label="年份" align="center" prop="deptBudgetYear" />

      <el-table-column
        label="1月"
        align="center"
        prop="deptBudgetJanuary"
      />
      <el-table-column
        label="2月"
        align="center"
        prop="deptBudgetFebruary"
      />
      <el-table-column label="3月" align="center" prop="deptBudgetMarch" />
      <el-table-column label="4月" align="center" prop="deptBudgetApril" />
      <el-table-column label="5月" align="center" prop="deptBudgetMay" />
      <el-table-column label="6月" align="center" prop="deptBudgetJnue" />
      <el-table-column label="7月" align="center" prop="deptBudgetJuly" />
      <el-table-column label="8月" align="center" prop="deptBudgetAugust" />
      <el-table-column
        label="9月"
        align="center"
        prop="deptBudgetSeptember"
      />
      <el-table-column
        label="10月"
        align="center"
        prop="deptBudgetOctober"
      />
      <el-table-column
        label="11月"
        align="center"
        prop="deptBudgetNovember"
      />
      <el-table-column
        label="12月"
        align="center"
        prop="deptBudgetDecember"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />




    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import request from "@/utils/request";
import { getToken } from "@/utils/auth";
import { importTemplate } from "@/api/system/user";
export default {
  name: "Dpt",
  created() {
    this.getList();
  },
  data() {
    return {
      // 片区
      pq: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址 api
        url: process.env.VUE_APP_BASE_API + "/budget/dpt/upload"
      },
      ids: [],
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 公司预算导入列表
      budgetList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vo: {

        }
      },
      

    };
  },
  mounted() {
    request({
      url: '/dict/area',
      method: 'get',
    }).then((res)=>{
      this.pq = res.data.data;
      //console.log(this.pq,'====--->>');
    })
  },
  methods: {
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 下载模板操作 */
    importTemplate() {
      // api 替换掉下面的
      request({
        url: '/budget/dpt/exportExample',
        method: 'get',
      }).then(response => {
        this.download(response.msg);
      });
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有公司年度预算导入 数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          // api
          return request({
            url: '/budget/dpt/export2',
            method: 'get',
          })
          // return exportBudget(queryParams);
        }).then(response => {
          this.download(response.msg);
          //console.log(this.download, response);
          // this.download(response.msg);
        }).catch(function() {});
    },
    handleDelete(row) {
        const companyBudgetIds = row.deptBudgetId || this.ids;
        //console.log(row,'--->>>')
        this.$confirm('是否确认删除公司年度预算导入 编号为"' + companyBudgetIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
            // api del
          return request({
              url: '/budget/dpt/remove',
              method: "post",
              data:{
                ids: companyBudgetIds.join(','),
              }
            });
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch((r)=>{
            this.msgError(r);
        });

    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deptBudgetId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;

     // console.log(this.ids, this.single, this.multiple,'---->>');
    },
    getList() {
      this.loading = true;
      request({
        url: "/budget/dpt",
        method: "post",
        data: this.queryParams
      }).then(response => {
        response = response.data;
        this.budgetList = response.data;
        this.total = response.total;
        this.loading = false;

        console.log(this.budgetList, this.total, "@@@@");
      });
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    }
  }
};
</script>
